package com.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;

/**
 * @author Create By Liuqz
 * @date 2018/11/14 11:19
 */
public class LambdaUsage {

    private static List<Apple> filter(List<Apple> apples, Predicate<Apple> predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : apples) {
            if (predicate.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    private static List<Apple> filterByWeight(List<Apple> apples, LongPredicate predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : apples) {
            if (predicate.test(apple.getWeight())) {
                result.add(apple);
            }
        }
        return result;
    }

    private static List<Apple> filterByBiPredicate(List<Apple> apples, BiPredicate<String, Integer> predicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : apples) {
            if (predicate.test(apple.getColor(), apple.getWeight())) {
                result.add(apple);
            }
        }
        return result;
    }

    private static void simpleBiConsumer(String c, List<Apple> source, BiConsumer<Apple, String> consumer) {
        for (Apple a : source) {
            consumer.accept(a, c);
        }
        System.out.println("Hello");
    }

    private static void simpleTestConsumer(List<Apple> source, Consumer<Apple> consumer) {
        for (Apple apple : source) {
            consumer.accept(apple);
        }
    }


    public static String testFunction(Apple apple, Function<Apple, String> function) {
        return function.apply(apple);
    }

    public static Apple testBiFunction(String color, int weigth, BiFunction<String, Integer, Apple> function) {
        return function.apply(color, weigth);
    }

    private static void process(Runnable runnable) {
        runnable.run();
    }

    public static void main(String[] args) {
//        Runnable r1 = () -> System.out.println("Hello");
//
//        Runnable r2 = new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("Hello");
//            }
//        };
//
//        process(r1);
//        process(r2);
//        process(() -> System.out.println("Hello"));

        List<Apple> appleList = Arrays.asList(new Apple("green",150),new Apple("yellow",130));

//        List<Apple> resultList = filter(appleList,apple -> apple.getColor().equals("green"));

//        List<Apple> resultList = filterByWeight(appleList,w -> w > 100);

//        List<Apple> resultList = filterByBiPredicate(appleList, (y,c) -> y.equals("yellow") && c > 100);

//        simpleTestConsumer(appleList, apple -> System.out.println(apple));

//        simpleBiConsumer("XXX", appleList, (a, s) -> System.out.println(s + a.getColor() + ":Weight=>" + a.getWeight()));

//        System.out.println(resultList);

//        String result = testFunction(new Apple("red", 120), (a) -> a.toString());

        Apple apple = testBiFunction("Bule", 120, (s, w) -> new Apple(s, w));
        System.out.println(apple);
    }
}
